package com.guo.pet_store.controller;

import com.guo.pet_store.admin.pojo.ShopCar;
import com.guo.pet_store.admin.pojo.User;
import com.guo.pet_store.admin.service.ShopService;
import com.guo.pet_store.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserLoginController {
    @Autowired
    UserService userService;
    @Autowired
    ShopService shopService;
    @PostMapping("/login")
    public String login(@RequestParam String username
            , @RequestParam String password,@RequestParam String check,
                        HttpSession session, RedirectAttributes redirectAttributes,Model model){
        //判断验证码是否错误
        if (! session.getAttribute("randCheckCode").toString().equalsIgnoreCase(check)) {
            model.addAttribute("message","验证码错误!");
            return "login";
        }
        User user = userService.checkUser(username,password);
        if (user!=null){
            session.setAttribute("user",user);
            return "redirect:/admin";
        }else {
            redirectAttributes.addFlashAttribute("loginMessage","用户名或者密码错误");
            return "redirect:/index";
        }
    }

    @GetMapping("/myInfo")
    private String toMyInfo(Model model,HttpSession httpSession){
        User user = (User) httpSession.getAttribute("user");
        if(user == null){
            model.addAttribute("message","先登录才能查看个人中心");
            return "login";
        }else {
            model.addAttribute("user",user);
            return "myInfo";
        }
    }

    @PostMapping("/updateUser")
    private String updateUser(User user,Model model,HttpSession httpSession){
        User oldUser = (User)httpSession.getAttribute("user");
        userService.updateUser(user,oldUser.getId());
        model.addAttribute("message","更改成功!");
        return "/myInfo";
    }

    @GetMapping("/register")
    public String getRegister(){
        return "register";
    }

    @PostMapping("/register")
    public String register(User user,Model model,@RequestParam String check,HttpSession session,@RequestParam  String Spassword){
        //判断验证码是否错误
        if (! session.getAttribute("randCheckCode").toString().equalsIgnoreCase(check)) {
            model.addAttribute("message","验证码错误!");
            return "register";
        }        //判断两次密码是否正确
        if(!user.getPassword().equals(Spassword)){
            model.addAttribute("message","两次密码不相同");
            return "register";
        }
        User userByName = userService.getUserByName(user.getUsername());
        //判断用户名是否存在
        if(userByName != null){
            model.addAttribute("message","该用户名已存在");
            return "register";
        }else {
            user.setEditor(false);
            user.setCreateDate(new Date());
            userService.addUser(user);
            User addedUser = userService.getUserByName(user.getUsername());
            //与购物车关联
            shopService.connectToUser(addedUser.getId());
            ShopCar shopCarByUser = shopService.findShopCarByUser(addedUser);

            user.setShopCar(shopCarByUser);
            userService.updateUser(user,user.getId());
            model.addAttribute("message","注册成功");
            return "register";
        }
    }
    @ResponseBody
    @GetMapping("/valid")
    public Map isValid(String username){
        Map<String,Boolean > map= new HashMap<>();
        if (userService.isUsed(username)){
            map.put("used",true);
        }else {
            map.put("used",false);
        }
        return map;
    }
    @ResponseBody
    @GetMapping("/isUsed")
    public Map isUsed(String username){
        Boolean used = userService.isUsed(username);
        HashMap<String, Boolean> result = new HashMap<>();
        if (used){
            result.put("message",true);
        }else
            result.put("message",false);
        return result;
    }
}
